import request from "./request";
import { getStore } from "@/utils/store";
import { client_id } from '@/const/const'
// 收藏|取消收藏
export const docStar = (id) => {
    return request({
      url: `/mgr/document/dcLibrary/collect/${id}`,
      method: 'put'
    })
}

export const getByIdShareInfo = (id)=>{
  return request({
    url:`/mgr/document/dcLibrary/share/get/${id}`,
    method:'get',
    isNotCancel:true
  })
}

// 分享设置
export function shareSetting(data) {
  return request({
    url:`/mgr/document/dcLibrary/share/setting`,
    method:'post',
    data: data,
    headers:{
      dcLibraryId:data.id
    }
  })
}

// 重命名知识库-目录-文档，或设置知识库
export function setLibrary(data) {
  return request({
    url:`/mgr/document/dcLibrary`,
    method:'put',
    data: data,
    headers:{
      MessageDcLibraryId:data.id,
      dcLibraryId:data.id
    }
  })
}

//添加留言
export function leaveMsg(data) {
  return request({
    url:`/mgr/document/dcLibrary/comment/save`,
    method:'post',
    data: data
  })
}

//点赞操作
export function giveGood(params) {
  return request({
    url:`/mgr/document/dcLibrary/like`,
    method:'post',
    params: params
  })
}

// 文件操作
export function getFileUrl(data){
  let userInfo = getStore({name: 'userInfo'})
  data.isToken = !!userInfo
  return request({
    url:!userInfo?`/mgr/document/no/auth/get/url/${data.id}`:`/mgr/document/dcLibrary/file/get/url/${data.id}`,
    method:'get',
    params:data,
    headers:{
      dcLibraryId:data.id,
      documentId:data.id,
      'is-cancel-token':'isCancelToken'
    }
  })
}

//获取点赞人员列表
export function getLike(params) {
  return request({
    url:`/mgr/document/dcLibrary/like/list`,
    method:'get',
    params: params
  })
}

// 收藏|取消收藏
export const getStarAll = () => {
  return request({
    url: `/mgr/document/dcLibrary/collectAll`,
    method: 'get'
  })
}

// 校验分享链接
export function checkShare(data) {
  return request({
    url:`/mgr/document/no/auth/dcLibrary/share/check`,
    method:'post',
    data: data
  })
}

// 获取知识库信息
export function getInfo(id){
  let userInfo = getStore({name: 'userInfo'})
  return request({
    url:!userInfo?`/mgr/document/no/auth/dcLibrary/info/${id}`:`/mgr/document/dcLibrary/info/${id}`,
    method:'get',
    headers: {
      dcLibraryId:id,
      documentId:id
    },
  })
}

// 获取二进制流
export const getFileByte = (data,responseType = 'blob')=>{
  return request({
    url:`/mgr/document/dcLibrary/file/get/file/byte/${data.id}`,
    method:'get',
    responseType:responseType,
    headers:{
      dcLibraryId:data.id
    }
  })
}

// 保存文档
export const saveContent = (contentId, data) => {
  return request({
    url: `/mgr/document/dcLibrary/save/content/${contentId}`,
    method: 'post',
    data: data,
    headers:{
      MessageDcLibraryId:contentId,
      dcLibraryId:contentId
    }
  })
}

// 预览文档
export const preview = (id) => {
  let userInfo = getStore({name: 'userInfo'})
  return request({
    url: !userInfo?`/mgr/document/no/auth/document/preview/${id}`:`/mgr/document/dcLibrary/preview/document/${id}`,
    method: 'get',
    headers:{
      MessageDcLibraryId:id,
      dcLibraryId:!userInfo?null:id,
      documentId:!userInfo?null:id,
    }
  })
}

// onlyOffice 新建文件
export function createOnlyOfficeFile(data){
  return request({
    url:`/mgr/document/dcLibrary/office/add`,
    method:'post',
    headers:{
      MessageDcLibraryId:data.id
    },
    data:data
  })
}

// 分享获取详情
export const getShareInfo = (id,TokenName = 'isCancelToken') => {
  let userInfo = getStore({name: 'userInfo'})
  return request({
    // url: !userInfo?`/mgr/document/no/auth/document/preview/${id}`:`/mgr/document/no/auth/dcLibrary/share/document/preview/${id}`,
    url:`/mgr/document/no/auth/document/preview/${id}`,
    method: 'get',
    headers:{
      'is-cancel-token':TokenName,
      dcLibraryId:id
    }
  })
}

// 获取凭证
export function getCertificate(data){
  return request({
    url: `/mgr/jvs-auth/largeFile/uploadCertificate/${data.bucketName}/${data.filename}/${data.num}`,
    method: 'get',
    params:data
  })
}

// 获取服务资源
export const getDynamicResource = () => {
  return request({
    url: `/mgr/jvs-auth/dynamicResource`,
    method: "get",
    params:{
      client_id:client_id
    }
  });
}

// 获取用户信息
export const getUserByCookie = (data) => {
  return request({
    url: `/auth/token`,
    method: "get",
    params:data
  });
};

// 对文件及其目录进行搜索
export function documentSearch(params) {
  return request({
    url: `/mgr/document/dcLibrary/document/search`,
    method: "get",
    params: params
  });
}

// 文件导入
export function fileImport(data,id,onUploadProgressBack){
  return request({
    url: `/mgr/document/dcLibrary/upload/${id}`,
    method: 'post',
    headers: {
      MessageDcLibraryId:id,
      dcLibraryId:id,
      serialize:false,
      type:"FormData",
      "Content-Type": "application/x-www-form-urlencoded"
    },
    data:data,
    isNotCancel:true,
    onUploadProgress(progressEvent){
      let val= (progressEvent.loaded / progressEvent.total * 100).toFixed(0);
      if(onUploadProgressBack && typeof onUploadProgressBack == 'function'){
        onUploadProgressBack(val);
      }
    }
  })
}
//
// 文件夹上传
export function bigFileUpload(data,bucketName='jvs-public',onUploadProgressBack,businessId){
  return request({
    url: `/mgr/jvs-auth/upload/${bucketName}`,
    method: 'post',
    headers: {
      businessId:id,
      type:"FormData",
      "Content-Type": "application/x-www-form-urlencoded"
    },
    isNotCancel:true,
    data:data,
    onUploadProgress(progressEvent){
      let val= (progressEvent.loaded / progressEvent.total * 100).toFixed(0);
      if(onUploadProgressBack && typeof onUploadProgressBack == 'function'){
        onUploadProgressBack(val);
      }
    }
  })
}
// 保存文件
export function saveFile(id,data){
  return request({
    url:`/mgr/document/dcLibrary/save/file/${id}`,
    method:'post',
    data,
    headers:{
      MessageDcLibraryId:id,
      dcLibraryId:id
    }
  })
}

// 获取当前的上传文件夹的key
export const getUploadFolderKey = ()=>{
  return request({
    url:'/mgr/document/update/folder/get/folder/key',
    method:'get'
  })
}

// 上传文件绑定
export const bindUploadFile = (data)=>{
  return request({
    url:'/mgr/document/update/folder/update/file',
    method:'post',
    data:data,
    isNotCancel:true
  })
}

// 通知是否到当前文件夹的最后一个
export const sendDirLastFile = (data)=>{
  return request({
    url:`/mgr/document/update/folder/${data}`,
    method:'get'
  })
}

export function getSelectedUsers(data) {
  return request({
    url: `/mgr/jvs-auth/user/all`,
    method: "post",
    data: data
  });
}
// 分页查询用户
export function getUserListPage(params){
  return request({
    url: `/mgr/jvs-auth/user/user/selected/page`,
    method: 'get',
    params: params
  })
}
// 根据类型返回树数据
export function getUserTreeType(data){
  return request({
    url: `/mgr/jvs-auth/user/user/selected`,
    method:'post',
    data: data
  })
}

// 获取所有用户
export function getUserListAll() {
  return request({
    url: `/mgr/jvs-auth//user/all`,
    method: "GET"
  });
}

// 获取下载次数和浏览次数
export const getDownViewNum = (id)=>{
  return request({
    url:`/mgr/document/statistics/${id}`,
    method:'get'
  })
}

// 获取文件统计数
export const getKnowFileNum = (id)=>{
  return request({
    url:`/mgr/document/statistics/document/total/${id}`,
    method:'get',
  })
}

// 获取文件大小
export const getFilSize = (id)=>{
  return request({
    url:`/mgr/document/statistics/document/file/size/${id}`,
    method:'get'
  })
}

// 获取文档库的控制台
export const getControl = ()=>{
  return request({
    url:`/mgr/document/statistics/index`,
    method:'get'
  })
}

// 查询文档统计
export const getStatistics = (data)=>{
  return request({
    url:`/mgr/document/statistics/index/add/statistics`,
    method:'get',
    params:data
  })
}

// 文件转换
export function fileTransition(data){
  return request({
    url:`/mgr/document/util/file/transition`,
    method:'post',
    data
  })
}
// 获取文件的预览地址
export function getFileNameUrl(data){
  return request({
    url:`/mgr/document/file/link/${data.bucketName}`,
    method:'get',
    params:data
  })
}
// ocr识别
export function ocrRecognition(data){
  return request({
    url:`/mgr/document/util/ocr`,
    method:'post',
    data
  })
}

// 文件合并
export function filesMer(data){
  return request({
    url:`/mgr/document/util/merge`,
    method:"post",
    data
  })
}

// 获取分享链接参数
export function getSharParams(data){
  return request({
    url:`/mgr/document/no/auth/link/${data}`,
    method:'get'
  })
 }
 // 文件保存
export function fileTransSave(id,data){
  return request({
    url:`/mgr/document/util/transition/save/file/${id}`,
    method:'post',
    headers:{
      serialize:false,
      type:"FormData",
    },
    data
  })
}
// ocr保存
export function ocrSaveContent(id,data){
  return request({
    url:`/mgr/document/util/ocr/save/${id}`,
    method:'post',
    headers:{
      serialize:false,
      type:"FormData",
    },
    data
  })
}
// 删除用户留言
export function delMsg(id) {
  return request({
    url:`/mgr/document/dcLibrary/comment/del/${id}`,
    method:'delete',
  })
}

// 上传封面

export function uploadTemplateCover(data){
  return request({
    url:`/mgr/jvs-auth/upload/document-mgr`,
    method:'post',
    data:data,
    headers: {
      businessId:data.id,
      serialize:false,
      'type':'FormData',
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 获取应用公告列表
export const getBulletinList = () => {
  return request({
    url: `/mgr/jvs-auth/index/bulletin/${client_id}`,
    method: 'get'
  })
}
